#!/bin/bash
# $Id$

# wget update_tar
# mktmpdir; unzip 
# tar list; cp from list local files; save
# untar update
# prompt for new screen

. /etc/scripts/env
. /etc/scripts/common.fn

tmpdowndir="$(mktemp -d /tmp/tmp.XXXXXX)"
tmptestdir="$(mktemp -d /tmp/tmp.XXXXXX)"
source_addr="$(__get_desc_val $XN5_desc_dir/netinstall/NETWORK INSTALLER_UPDATE_URL)"
#source_addr="http://sourceforge.net/projects/nemonico/files/installer_update"
#update_file="/${XN5_version}/installer_update.tar.bz2"
update_file="installer_update-${XN5_version}-${slackware_arch}-${installer_for}.tar.bz2"
date="$(date "+%s")"

set -eET
set -o pipefail

if [ -s $tmpdowndir/${update_file} ]; then
	_warn_msg "Deleting previous file download ..."
	rm $tmpdowndir/${update_file}
fi

_msg "Downloading installer updates... timeout 20 seconds."
wget -U "XN5-installer" -P $tmpdowndir \
	-T 20  ${source_addr}/${update_file}

if [ ! -s $tmpdowndir/${update_file} ]; then
	_err_msg "Could not connect/download update file, or there are no updates, exiting..."
fi

cd $tmptestdir
tar tf $tmpdowndir/${update_file} > $tmptestdir/update_list

for f in $(<${tmptestdir}/update_list) ; do
	if [ -f /${f} -o -d /${f} ]; then
		echo $f >> ${tmptestdir}/update_list.n
		test -d /${f} && sed -e '$d' -i ${tmptestdir}/update_list.n
	fi
done

_msg "\nSaving orginal files ...\n"
cd /
tar -m -T ${tmptestdir}/update_list.n \
	-cjf /var/tmp/installer_original_${date}.tar.bz2

_msg "\nPerforming update ...\n"
cd /
tar -m -vxf $tmpdowndir/${update_file}

_msg "\n\nNow open another screen (Ctrl-a c).\n"

# vim: ts=2
# installer_update.sh: end
